package com.blrs.service.project.productInfo.impI;

import java.util.List;
import java.util.UUID;

import com.blrs.domain.ContractualProductInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blrs.mapper.ProjectProductInfoMapper;
import com.blrs.domain.ProjectProductInfo;
import com.blrs.service.project.productInfo.IProjectProductInfoService;

/**
 * 项目产品Service业务层处理
 * 
 * @author wangchao
 * @date 2023-12-04
 */
@Service
public class ProjectProductInfoServiceImpl implements IProjectProductInfoService 
{
    @Autowired
    private ProjectProductInfoMapper projectProductInfoMapper;

    /**
     * 查询项目产品
     * 
     * @param id 项目产品主键
     * @return 项目产品
     */
    @Override
    public ProjectProductInfo selectProjectProductInfoById(String id)
    {
        return projectProductInfoMapper.selectProjectProductInfoById(id);
    }

    /**
     * 查询项目产品列表
     * 
     * @param projectProductInfo 项目产品
     * @return 项目产品
     */
    @Override
    public List<ProjectProductInfo> selectProjectProductInfoList(ProjectProductInfo projectProductInfo)
    {
        return projectProductInfoMapper.selectProjectProductInfoList(projectProductInfo);
    }

    /**
     * 新增项目产品
     * 
     * @param projectProductInfo 项目产品
     * @return 结果
     */
    @Override
    public int insertProjectProductInfo(ProjectProductInfo projectProductInfo)
    {
        boolean flag = projectProductInfo.getProductId().contains(",");
        ProjectProductInfo info = new ProjectProductInfo();
        int count = 0;
        if(flag){
            String[] productId = projectProductInfo.getProductId().split(",");
            String[] productName = projectProductInfo.getProductName().split(",");
            String[] productType = projectProductInfo.getProductCategory().split(",");
            String[] productCode = projectProductInfo.getProductNumber().split(",");
            String[] productRange = projectProductInfo.getPricingRange().split(",");
            for (int i = 0; i < productId.length; i++) {
                projectProductInfoMapper.deleteProjectProductInfoByProductId(productId[i]);
                info.setId(UUID.randomUUID().toString());
                info.setProjectId(projectProductInfo.getProjectId());
                info.setProductId(productId[i]);
                info.setProductName(productName[i]);
                info.setProductCategory(productType[i]);
                info.setProductNumber(productCode[i]);
                if(productRange.length<productId.length){
                    info.setPricingRange("0");
                }else{
                    info.setPricingRange(productRange[i]);
                }

                count = projectProductInfoMapper.insertProjectProductInfo(info);
            }
        }else{
            projectProductInfoMapper.deleteProjectProductInfoByProductId(projectProductInfo.getProductId());
            info.setId(UUID.randomUUID().toString());
            info.setProjectId(projectProductInfo.getProjectId());
            info.setProductId(projectProductInfo.getProductId());
            info.setProductName(projectProductInfo.getProductName());
            info.setProductCategory(projectProductInfo.getProductCategory());
            info.setProductNumber(projectProductInfo.getProductNumber());
            info.setPricingRange(projectProductInfo.getPricingRange());
            count = projectProductInfoMapper.insertProjectProductInfo(info);
        }
        return count;
        //return projectProductInfoMapper.insertProjectProductInfo(projectProductInfo);
    }

    /**
     * 修改项目产品
     * 
     * @param projectProductInfo 项目产品
     * @return 结果
     */
    @Override
    public int updateProjectProductInfo(ProjectProductInfo projectProductInfo)
    {
        return projectProductInfoMapper.updateProjectProductInfo(projectProductInfo);
    }

    /**
     * 批量删除项目产品
     * 
     * @param ids 需要删除的项目产品主键
     * @return 结果
     */
    @Override
    public int deleteProjectProductInfoByIds(String[] ids)
    {
        return projectProductInfoMapper.deleteProjectProductInfoByIds(ids);
    }

    /**
     * 删除项目产品信息
     * 
     * @param id 项目产品主键
     * @return 结果
     */
    @Override
    public int deleteProjectProductInfoById(String id)
    {
        return projectProductInfoMapper.deleteProjectProductInfoById(id);
    }

    /**
     * 删除项目产品信息
     *
     * @param projectId 项目主键
     * @return 结果
     */
    @Override
    public int deleteProjectProductInfoByProjectId(String projectId)
    {
        return projectProductInfoMapper.deleteProjectProductInfoByProjectId(projectId);
    }

    /**
     * 删除项目产品信息
     *
     * @param ProductId 产品id
     * @return 结果
     */
    @Override
    public int deleteProjectProductInfoByProductId(String ProductId)
    {
        return projectProductInfoMapper.deleteProjectProductInfoByProductId(ProductId);
    }
}
